<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ManagerController
 *
 * @author Administrator
 */
class ManagerController extends Controller {

    public function actionIndex() {
        $model = new Manager();
        $this->model = $model;
        $this->performAjaxValidation($model);
        if (Yii::app()->request->getRequestType() == 'POST') {
            $model->attributes = Yii::app()->request->getPost('Manager');
            if ($model->save()) {
                $this->redirect($this->createUrl("index"));
            }
        }
        $this->render("index");
    }

    public function actionEdit() {
        $model = new Manager();
        $this->model = $model;
        $id = (int) Yii::app()->request->getParam('id');
        $modify = $model->findByPk($id);
        $this->performAjaxValidation($model);
        if (Yii::app()->request->getRequestType() == 'POST') {
            $modify->attributes = Yii::app()->request->getPost('Manager');
            if ($modify->save()) {
                $this->redirect($this->createUrl("index"));
            }
        }
        $this->render("edit", array('modify' => $modify));
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'user-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function actionDel() {
        $model = new Manager();
        $id = intval(Yii::app()->request->getParam("id"));
        if ($model->count($id, "admin_status='open' AND admin_group_id=1") > 1) {
            $model->deleteByPk($id);
        }
        $this->redirect(Yii::app()->request->getUrlReferrer());
    }

}

?>
